package com.enterprise.support.utility.generator.pub;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2011-1-26
 * Time: 16:00:41
 * To change this template use File | Settings | File Templates.
 */
public class GeneratorConfig {
    public final static File homeDirectory = new File(com.enterprise.support.utility.Configuration.LibDirectory);
    public final static File toolsProperties = new File(homeDirectory, "/java/src/config/tools.properties");
    public final static File jdbcProperties = new File(homeDirectory, "/java/src/config/jdbc.properties");

    static {
        if (!toolsProperties.getParentFile().exists()) {
            toolsProperties.getParentFile().mkdirs();
        }
    }

    public static Properties getProperties(File file) {
        Properties properties = new Properties();
        FileInputStream input = null;
        try {
            input = new FileInputStream(file);
            properties.load(input);
        } catch (Exception ex) {
            store(file, properties);
        } finally {
            if (null != input) {
                try {
                    input.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        return properties;
    }

    public static void store(File file, Properties properties) {
        FileOutputStream output = null;
        try {
            output = new FileOutputStream(file);
            output.flush();
            properties.store(output, "Enterprise Tools GeneratorConfig");
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(GeneratorConfig.jdbcProperties);
    }
}
